package uoc.biblioteca.mappings.dao;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import uoc.biblioteca.mappings.object.LlibreVo;
import uoc.biblioteca.mappings.object.RevistaVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class Revista.
 * @see uoc.biblioteca.mappings.dao.RevistaVo
 * @author Hibernate Tools
 */
public class RevistaHome {

	private static final Log log = LogFactory.getLog(RevistaHome.class);

	private final SessionFactory sessionFactory  = null;

	
	
	
	public RevistaVo findById(java.lang.Integer id) {
		log.debug("getting RevistaVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			RevistaVo instance = (RevistaVo) session.get("uoc.biblioteca.mappings.object.RevistaVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void saveNew(RevistaVo instance) {
		
		log.debug("save new RevistaVo instance");
		
		try {
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
		
		
	}
	
	//Mostrar todas las revistas solicitadas duplicadas por titol 
	public RevistaVo[] getAllRevistaDuplicatedByTitol() {
		
		/* will hold the books we are going to return later */
		try{
			List solicituds = new ArrayList();
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select d from uoc.biblioteca.mappings.object.RevistaVo as d group by d.titol having count(*) > 1 order by d.titol ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				RevistaVo revistaTemp = (RevistaVo) iter.next();
				solicituds.add(revistaTemp);
			}
			tx.commit();
			
			return (RevistaVo[]) solicituds.toArray(new RevistaVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
